/**
 * Created by Administrator on 2017/5/12 0012.
 */
layui.use('element', function(){
    var element = layui.element();

    //…
});

layui.use(['laypage', 'layer'], function(){
    var laypage = layui.laypage,layer = layui.layer;
    laypage({
        cont: 'fullpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function(obj, first){
            //得到了当前页，用于向服务端请求对应数据
            if(first!=true){//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var jsonObject = $('#jsonObject').val();
                if(jsonObject.length>0){
                    console.log(jsonObject);
                    var objs = eval('(' + jsonObject + ')');//将json转成对象
                    var condition = 'pageIndex='+pageIndex +'&';
                    condition+="croFeeVo.name="+objs.name+'&'+"croFeeVo.type="+objs.type+'&'+
                        "croFeeVo.starcost="+objs.starcost+'&'+"croFeeVo.endcost="+objs.endcost;
                    console.log(condition);
                    window.location.href ="/admin/fee/querylist?"+condition;
                } else {
                    window.location.href ="/admin/fee/showdetail?pageIndex="+pageIndex;
                }
            }
        }
    });
});

layui.use(['laypage', 'layer'], function() {
    var laypage = layui.laypage, layer = layui.layer;
    //地址分页
    laypage({
        cont: 'detailpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function(obj, first){
            //得到了当前页，用于向服务端请求对应数据
            var cusid=$('#croCustomerid').val();
            if(first!=true){//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                window.location.href ="/admin/fee/?pageIndex="+pageIndex+'&customerid='+cusid;
            }
        }
    });
});



//layer弹出层
layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active={
        update : function (othis) {
            //弹出即全屏
            var index = layer.open({
                    type: 2,
                    title: "修改费用信息",
                    shadeClose: true,
                    content: 'listFee?id='+othis.feeid,
                    area: ['100%', '100%'],
                    maxmin: false
                });
                layer.full(index);
        },



        delfee: function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/delefee.action",
                        type: 'post',
                        data: {
                            id: othis.feeid,
                            del_flag:"1"
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "删除成功") {
                                window.location.href = "/admin/fee/showdetail";
                                layer.msg("删除成功");
                            }else {
                                layer.msg("删除失败");
                            }
                        }
                    });

                }
            });
        },
    };


    $('.delsumit').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.feeid = $(this).attr('feeid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.updatefee').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.feeid = $(this).attr('feeid');
        active[method] ? active[method].call(this, othis) : '';
    });
});


layui.use('form', function(){
    var $ = layui.jquery, form = layui.form();

    //监听提交
    form.on('submit(addsubmit)', function (data) {
        console.log(data.field);
        var params = $.param({
            name: data.field.feename,
            type: data.field.type,
            cost: data.field.cost,
        },true);
        // console.log(params);
        var cost=$('#cost').val();


        function checkRates(str){
            var  r  =  /^\d+(\.\d+)?$/
            var Sure;
            if (!r.test(str)){
                Sure =0;//错误为0
            }else{
                Sure =1;
            }
            return Sure;
        }

        if(checkRates(cost)){
            $.ajax({
                url: '/addfee.action',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (data) {
                    console.log(data);
                    window.location.href = "/admin/fee/showdetail";
                    layer.msg("添加成功");
                    layer.closeAll();
                }
            });
        }else {
            layer.msg("请检查输入价值是否正整数");
        }


        return false;
    });


    form.on('submit(updatefee)', function (data) {
        console.log(data.field);
        var params = $.param({
            id:data.field.id,
            name: data.field.name,
            type: data.field.upcrofeetype,
            cost: data.field.cost,
        },true);
        var cost=$('#cost').val();
        console.log(params);

        function checkRates(str){
            var  r  =  /^\d+(\.\d+)?$/;
            var Sure;
            if (!r.test(str)){
                Sure =0;//错误为0
            }else{
                Sure =1;
            }
            return Sure;
        }


        if (checkRates(cost)) {
            $.ajax({
                url: '/updatefee.action',
                type: 'post',
                data: params,
                dataType: 'json',
                success: function (data) {
                    parent.window.location.reload();
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }
            });
        } else {
            layer.msg("请检查输入价值是否正整数");
        }

        return false;
    });

});
